@extends('admin.layout')
@section('content')
<!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                Data Tables
                <small>advanced tables
                	@if(session('info'))
                    <span id='info' class="text-red"> {{session('info')}}</span>

                @endif
                </small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                <li><a href="#">Tables</a></li>
                <li class="active">Data tables</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    

                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">商品列表</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">

                            <table id="example1" class="table table-bordered table-striped">
								<form action="{{ url('/admin/goods/index') }}">

								<div class="row">
									<div class="col-md-2">
										<!-- select -->
                               			<div class="form-group">
                                    		<select name="num" class="form-control">
			                                        <option 
			                                        	@if(!empty($request['num']) && 
			                                        	$request['num'] == 10)
															selected="selected"
														@endif>10
													</option>
			                                        <option
			                                        	@if(!empty($request['num']) && $request['num'] == 20)
															selected="selected"
														@endif>20
													</option>
			                                        <option
			                                      		@if(!empty($request['num']) && $request['num'] == 50)
															selected="selected"
														@endif>50
													</option>
			                                        <option
			                                        	@if(!empty($request['num']) && $request['num'] == 100)
															selected="selected"
														@endif>100
													</option>
			                                    </select>
										</div>
									</div>
									<div class="col-md-4 col-md-offset-6">
										<div class="input-group input-group">
                                			<input name="keywords" value="{{ $request['keywords'] or ''}}" type="text" class="form-control">
                    					<span class="input-group-btn">
                      						<button class="btn btn-info btn-flat" type="submit">搜索</button>
                    					</span>
                            			</div>
									</div>
								</div>
								</form>

                                <thead>
                                <tr>
                                    <th>商品ID</th>
                                    <th>商品名称</th>
                                    <th>商品图片</th>
                                    <th>类别</th>
                                    <th>价格</th>
                                    <th>库存</th>
                                    <th>销量</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($data as $row)	 
                                <tr>
                                    <td>{{ $row -> gid }}</td>
                                    <td class="td_name">{{ $row -> goodsname }}</td>
                                    <td><img src="{{ asset($row -> goodspic) }}" style="width:30px"></td>
                                    <td>{{ $row -> name }}</td>
                                    <td>{{ $row -> price }}</td>
                                    <td>{{ $row -> store }}</td>
                                    <td>{{ $row -> scnt }}</td>
                                    <td>
										@if($row -> status == 1)
                                    		新品
                                    	@elseif($row -> status == 2)
                                    		在售
                                        @else
                                            下架
                                    	@endif

                                    </td>
                                    <td>
	                                    <a href=" {{ url('/admin/goods/edit') }}/{{$row -> gid}}">编辑<a/>
	                                    <a href=" {{ url('/admin/goods/delete') }}/{{$row -> gid}}">删除</a>
                                    </td>
                                    
                                </tr>
                                @endforeach
                                </tbody>
                            </table>
                            {{ $data -> appends($request) ->links() }} 
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->
	<script>

		window.onload = function()
		{
			var isDbl = true;

			$.ajaxSetup({
			    headers: {
			        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			    }
			});

			$(".td_name").on('dblclick',function(){
				if(!isDbl)
				{
					return;
				}

				isDbl = false;

				var td = $(this);
				//获取ID
				var id =$(this).prev().html();	

				//获取原来的值
				var old_value = $(this).html();
				//创建input元素
				var inp = $('<input type="text" />');
				//将原来的值写入input
				inp.val(old_value);
				//将td改为input
				$(this).html(inp); 

				//直接选中
				inp.select();
				//失去焦点事件
				inp.on('blur',function(){
					//获取新值
					var new_value = $(this).val();
					$.ajax({
    					url: "{{ url('/admin/admin/changename') }}",
    					type: 'GET',
    					data:{id:id,name:new_value},
    					success:function(data)
    					{
    						if(data == '1')
    						{
    							td.html(new_value);
    						}else
    						{
    							td.html(old_value);
    						}
    					},
    					error:function(){

    					},
    					dataType:'text'
    				});
					isDbl = true;
				});

			});
			$('#info').fadeOut(1000);
		}
	</script>
@endsection